package com.jxs.android.weixi;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.util.Log;

import com.jxs.android.util.InstallHolder;

public abstract class Start_Older_Activity_Abstract_Loading extends LoadingMoreAbstractList {
	private ArrayList<String> packagNameList;
	private MyReceiver receiver = new MyReceiver();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initpackagNameList();
		register();
	}

	// 监听系统新安装程序的广播
	private void register() {
		IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);// 注册广播机制
		filter.addDataScheme("package"); // 必须添加这项，否则拦截不到广播
		registerReceiver(receiver, filter);
	}

	private void unRegister() {
		unregisterReceiver(receiver);
	}

	protected boolean installed() {
		boolean installed = detectApk(InstallHolder.INSTALL_PACKAGE);// 检查是否已经安装
		if (installed) {
			try {
				PackageManager manager = this.getPackageManager();
				PackageInfo info = manager.getPackageInfo(InstallHolder.INSTALL_PACKAGE, 0);
				if (InstallHolder.INSTALL_PACKAGE_VESION.equals(info.versionName)) {
					return true;
				} else {
					install(true);
					return false;
				}
			} catch (NameNotFoundException e) {
			}
		} else {
			File fileDir = getFilesDir();
			final String cachePath = fileDir.getAbsolutePath() + "/" + InstallHolder.INSTALL_PACKAGE_APK;
			InstallHolder.retrieveApkFromAssets(this, InstallHolder.INSTALL_PACKAGE_APK, cachePath);
			InstallHolder.showInstallConfirmDialog(this, cachePath, false);
		}
		return installed;
	}

	public void startOldNew1Activity(String serv_id, String cust_id, String cust_nbr) {
		boolean installed = detectApk(InstallHolder.INSTALL_PACKAGE);// 检查是否已经安装
		if (installed) {// 已经安装直接起动
			try {
				PackageManager manager = this.getPackageManager();
				PackageInfo info = manager.getPackageInfo(InstallHolder.INSTALL_PACKAGE, 0);
				if (InstallHolder.INSTALL_PACKAGE_VESION.equals(info.versionName)) {
					startActivity(getStartIntent(serv_id, cust_id, cust_nbr, false));
				} else {
					install(true);
				}
			} catch (NameNotFoundException e) {
			}
		} else {// 未安装先安装
			install(false);
		}
	}

	private void install(boolean update) {
		File fileDir = getFilesDir();
		final String cachePath = fileDir.getAbsolutePath() + "/" + InstallHolder.INSTALL_PACKAGE_APK;
		InstallHolder.retrieveApkFromAssets(this, InstallHolder.INSTALL_PACKAGE_APK, cachePath);
		InstallHolder.showInstallConfirmDialog(this, cachePath, update);
	}

	public static Intent getStartIntent(String serv_id, String cust_id, String cust_nbr, boolean hindTitle) {
		Intent intent = new Intent();
		intent.setComponent(new ComponentName(InstallHolder.INSTALL_PACKAGE, "com.telecom.weixi.DirectTo"));
		intent.putExtras(getOlderBundle(serv_id, cust_id, cust_nbr, hindTitle));
		return intent;
	}

	public static Bundle getOlderBundle(String serv_id, String cust_id, String cust_nbr, boolean hindTitle) {
		Bundle bundle = new Bundle();
		bundle.putString("mUserId", WeixiApplication.getApplication().getUserId());
		bundle.putString("mLatnId", WeixiApplication.getApplication().getLatnId());
		bundle.putString("passwd", WeixiApplication.getApplication().getPasswd());
		bundle.putString("ticket", WeixiApplication.getUserInfo().getTicket());
		bundle.putString("url", WeixiApplication.WSDL_URL);
		bundle.putString("namespace", WeixiApplication.WSDL_NAME_SPACE);
		bundle.putString("activity", "New1Activity");

		bundle.putString("serv_id", serv_id);
		bundle.putString("cust_id", cust_id);
		bundle.putString("cust_nbr", cust_nbr);
		bundle.putBoolean("hindTitle", hindTitle);

		return bundle;
	}

	// 捆绑安装

	/**
	 * 检测是否已经安装
	 * 
	 * @param packageName
	 * @return true已安装 false未安装
	 */
	private boolean detectApk(String packageName) {
		return packagNameList.contains(packageName.toLowerCase());

	}

	private void initpackagNameList() {
		// 初始化小模块列表
		packagNameList = new ArrayList<String>();
		PackageManager manager = this.getPackageManager();
		List<PackageInfo> pkgList = manager.getInstalledPackages(0);
		for (int i = 0; i < pkgList.size(); i++) {
			PackageInfo pI = pkgList.get(i);
			packagNameList.add(pI.packageName.toLowerCase());
		}

	}

	/**
	 * 
	 * 设置广播监听
	 * 
	 */
	private class MyReceiver extends BroadcastReceiver {
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(Intent.ACTION_PACKAGE_ADDED)) {

				String packName = intent.getDataString().substring(8);

				Log.e(intent.getDataString() + "====", packName);
				// package:cn.oncomm.activity cn.oncomm.activity
				// packName为所安装的程序的包名
				packagNameList.add(packName.toLowerCase());

				// 删除file目录下的所有以安装的apk文件
				File file = getFilesDir();
				File[] files = file.listFiles();
				for (File f : files) {
					if (f.getName().endsWith(".apk")) {
						f.delete();
					}
				}

			}
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unRegister();
	}

}
